/*
 * author      : 吴中勤 
 * date        : 2018/12/18
 * description : TODO
 */

package org.wzq.learn;

import sun.misc.Unsafe;

import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.List;

/*
cd src
javac src/org/wzq/learn/DirectMemoryOOM.java
java -Xms20M -Xmx20M -XX:MaxDirectMemorySize=10M org/wzq/learn/DirectMemoryOOM

【output】
并没有溢出，但是观察主机，内存剧烈波动，虚拟内存频繁写
*/

public class DirectMemoryOOM {
    private static final int _1MB = 1024 * 1024;

    public static void main(String[] args) throws IllegalAccessException {
        Field unsafeField = Unsafe.class.getDeclaredFields()[0];
        unsafeField.setAccessible(true);

        Unsafe unsafe = (Unsafe) unsafeField.get(null);
        while (true) {
            unsafe.allocateMemory(_1MB);
        }
    }
}
